﻿var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
    for (var i = 0; i < numCPUs; i++) {
        let s = cluster.fork();
        s.process.a = 20
    }
    cluster.on('death', function (worker) {
        console.log('worker ' + worker.pid + ' died');
        s=cluster.fork();
        s.send(">>>");
    });
    global.a = 5000;
} else {
    // 不同进程
    http.Server(function (req, res) {
        res.writeHead(200);
        res.end("hello world\n");
    }).listen(8000);
    setTimeout(() => { console.log(process.a) }, 1000)
}
cluster.on('listening', (worker, address) => {
    console.log(
        `A worker is now connected to ${address.address}:${address.port}`);
});
cluster.on('message', (worker, message, handle) => {
      //  debugger
      console.log("onmessage>>")
        console.log(arguments);
    })

